package fm.dongman.animefm.view.fragment;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import org.jetbrains.annotations.NotNull;

import fm.dongman.animefm.R;
import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.contract.SheetCreateContract;
import fm.dongman.animefm.view.util.SystemHelper;
import fm.dongman.animefm.view.util.ToastHelper;
import fm.dongman.modellib.StaticArea.Injection;
import fm.dongman.modellib.model.SheetModel;
import fm.dongman.presenterlib.presenter.SheetCreatePresenter;

/**
 * @description 创建漫单页面
 * Created by alex on 2017/11/27.
 */

public class CreateSheetFragment extends BaseFragment implements SheetCreateContract.ISheetCreateView, View.OnClickListener {

    private static final String EXTRA_SHEET_ID = "sheet_id";

    private String mSheetId;    //漫单id
    private boolean mIsEdit = false;    //是否为编辑漫单模式，true为编辑模式

    private Toolbar mToolbar;
    private EditText mEtTitle;
    private EditText mEtDescription;
    private Button mBtnSubmit;

    private SheetCreatePresenter mPrenster;

    public static CreateSheetFragment getInstance(String sheetId) {
        CreateSheetFragment fragment = new CreateSheetFragment();
        Bundle args = new Bundle();
        args.putString(EXTRA_SHEET_ID, sheetId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mSheetId = getArguments().getString(EXTRA_SHEET_ID);
            //如果id有值，说明为编辑模式
            if (mSheetId != null) {
                mIsEdit = true;
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View layoutView = inflater.inflate(R.layout.fragment_create_sheet, null);
        initComponents(layoutView);
        new SheetCreatePresenter(this, Injection.provideSheetCreateRepository());
        return layoutView;
    }

    @Override
    public void initData() {

    }

    @Override
    public void bindData() {
        if (mIsEdit) {
            mPrenster.setEditSheetId(mSheetId);
        }
    }

    /**
     * 初始化组件
     *
     * @param layoutView
     */
    private void initComponents(View layoutView) {
        mToolbar = (Toolbar) layoutView.findViewById(R.id.mTbCreateSheet);
        mBtnSubmit = (Button) layoutView.findViewById(R.id.mBtnCreateSheetSubmit);
        mBtnSubmit.setOnClickListener(this);
        mEtTitle = (EditText) layoutView.findViewById(R.id.mEtCreateSheetTitle);
        mEtDescription = (EditText) layoutView.findViewById(R.id.mEtCreateSheetDesc);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });

        if (Build.VERSION.SDK_INT >= 21) {
            mToolbar.setPadding(0, SystemHelper.INSTANCE.getStatusBarHeight(getActivity()), 0, 0);
        }
        if (mIsEdit)
            mToolbar.setTitle("编辑漫单");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //提交
            case R.id.mBtnCreateSheetSubmit:
                //获取输入框中填写的信息
                String title = mEtTitle.getText().toString().trim();
                String desc = mEtDescription.getText().toString().trim();
                if (title.length() < 2) {
                    ToastHelper.getInstance(getActivity()).showWarn("标题长度不够");
                    return;
                }

                mBtnSubmit.setEnabled(false);

                SheetModel sheet = new SheetModel();
                sheet.setMandanTitle(title);
                sheet.setSummary(desc);

                //如果是编辑模式，设置id
                if (mIsEdit) {
                    sheet.setMandanId(mSheetId);
                    mPrenster.editSheet(sheet);
                } else {
                    mPrenster.createSheet(sheet);
                }
                break;
        }
    }

    @Override
    public void showCreateSucceed() {
        ToastHelper.getInstance(getActivity()).showSucceed("创建成功");
        getActivity().finish();
        mBtnSubmit.setEnabled(true);
    }

    @Override
    public void showCreateFailed() {
        ToastHelper.getInstance(getActivity()).showSucceed("创建失败");
        mBtnSubmit.setEnabled(true);
    }

    @Override
    public void showEditSheetInfo(IModel.ISheetModel iSheetModel) {
        SheetModel sheetModel = (SheetModel) iSheetModel;
        mEtTitle.setText(sheetModel.getMandanTitle());
        mEtDescription.setText(sheetModel.getSummary());
    }

    @Override
    public void showEditState(boolean b) {
        mBtnSubmit.setEnabled(true);
        ToastHelper.getInstance(getActivity()).showWarn("修改成功" + (b ? "成功" : "失败"));
    }

    @Override
    public void setPresenter(SheetCreateContract.ISheetCreatePresenter iSheetCreatePresenter) {
        mPrenster = (SheetCreatePresenter) iSheetCreatePresenter;
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }

    @NotNull
    @Override
    public Context getViewContext() {
        return getActivity();
    }

    @Override
    public void showNoNetwork() {
       // ToastHelper.getInstance(getActivity()).showWarn("无网络，获取数据失败");
    }

    @Override
    public void showRefreshFailed() {

    }
}
